package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RequestMapping("/admin/dish")
@RestController("adminDishController")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {

    @Autowired
    DishService dishService;

    @Autowired
    DishMapper dishMapper;

    @ApiOperation("新增菜品")
    @PostMapping
    public Result<String> addDish(@RequestBody DishDTO dishDTO){
        dishService.save(dishDTO);
        return Result.success();
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        return Result.success(dishService.page(dishPageQueryDTO));
    }

    @ApiOperation("删除菜品(批量删除)")
    @DeleteMapping
    public Result<String> deleteDish(@RequestParam List<Long> ids){
        log.info("菜品批量删除: {}",ids);
        dishService.deleteDish(ids);
        return Result.success();
    }

    @CacheEvict(cacheNames = "dishCache", allEntries = true)
    @ApiOperation("启用停用菜品")
    @PostMapping("/status/{status}")
    public Result<String> startOrStopDish(@PathVariable Integer status, Long id){
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishService.updateDish(dish);
        return Result.success();
    }

    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> selectDishById(@PathVariable Long id){
        DishVO dish = dishService.selectDishById(id);
        return Result.success(dish);
    }

    @CacheEvict(cacheNames = "dishCache", allEntries = true)
    @ApiOperation("修改菜品")
    @PutMapping
    public Result<String> updateDish(@RequestBody DishDTO dishDTO){
        dishService.updateDishALL(dishDTO);
        return Result.success();
    }

    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<Dish[]> selectDishByCategoryId(Long categoryId){
        return Result.success(dishService.selectDishByCategoryId(categoryId));
    }
}
